package com.project.service;

import com.project.dao.RoleMapper;
import com.project.entity.Role;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 14:57
 * @param:
 * @return:
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class RoleServiceImpl extends BaseService implements RoleService{

    @Autowired
    private RoleResourceService roleResourceService;

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private  RoleMapper roleMapper;
    /**
     * author: WheelChen
     * date: 2017/11/5
     * function: 添加单条角色表记录
     *
     * @param role
     * @return
     */
    @Override
    public int addOne(Role role) {
        return roleMapper.insertSelective(role);
    }

    /**
     * author: WheelChen
     * date: 2017/11/5
     * function: 根据id删除单条记录
     *
     * @param id 角色表主键
     * @return
     */
    @Override
    public int removeOneById(Integer id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/11/5
     * function: 根据id修改单条记录
     *
     * @param role
     * @return
     */
    @Override
    public int modifyOneById(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * author: WheelChen
     * date: 2017/11/5
     * function: 根据条件模糊查找
     *
     * @param role
     * @return
     */
    @Override
    public List<Role> findBatchByCondition(Role role) {
        return roleMapper.selectBatchByCondition(role);
    }

    @Override
    public int removeBatchByIdList(List idList) {

        roleMapper.deleteBatchByPk(idList); //删除角色记录

        //删除角色资源表中的相应记录;删除用户角色中的相应记录
        for(int i = 0 ; i < idList.size() ; i++) {
            roleResourceService.delByRoleId(Integer.valueOf(idList.get(i).toString()));
            userRoleService.removeBatchByRoleId(Integer.valueOf(idList.get(i).toString()));
        }

        return 1;
    }

    /**
     * author: WheelChen
     * date: 2017/11/7
     * function: 根据主键查找单个角色
     *
     * @param id
     * @return
     */
    @Override
    public Role findOneById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }
}
